//
//  PickCityView.m
//  New_Runway
//
//  Created by mack on 2018/1/27.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "PickCityView.h"
#import "PrevinceModel.h"
#import "CityModel.h"

@interface PickCityView ()<UIPickerViewDataSource, UIPickerViewDelegate>

//@property (nonatomic, strong, nullable)NSArray *areaDataSource;

@property (nonatomic, strong, nullable)NSMutableArray *provinceArray;
@property (nonatomic, strong, nullable)NSMutableArray *AllProvinceArray;

@property (nonatomic, strong, nullable)NSMutableArray *cityArray;
@property (nonatomic, strong, nullable)NSMutableArray *AllCityArray;


@property (nonatomic, strong, nullable)NSMutableArray *selectedArray;//当前选中的数组

@property (nonatomic, strong, nullable)NSString *selectProvince;
@property (nonatomic, strong, nullable)NSString *selectProvinceId;

@property (nonatomic, strong, nullable)NSString *selectCity;
@property (nonatomic, strong, nullable)NSString *selectCityId;

@end

@implementation PickCityView
- (void)initPickView
{
    [super initPickView];
    [self loadProvinceList];
    
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.provinceArray.count;
    }else  {
        return self.cityArray.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 36;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {

        self.selectProvince = self.provinceArray[row];
        PrevinceModel *model = self.AllProvinceArray[row];
        self.selectProvinceId = model.provinceId;
        [self loadCityList:model.provinceId];        
    }else if (component == 1) {
        self.selectCity = self.cityArray[row];
        CityModel *model = self.AllCityArray[row];
        self.selectCityId = model.cityId;
    }else{
        
    }
    
//    [self refreshSelectAreaData];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    
    
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor =[UIColor grayColor];
        }
    }
    NSString *text;
    if (component == 0) {
        text =  self.provinceArray[row];
    }else if (component == 1){
        
        if (self.cityArray.count > row) {
            text =  self.cityArray[row];
        }else{
            
        }
    }else{
        
    }
    
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:16];
    label.text = text;
    
    return label;
}


- (void)clickConfirmButton
{
    
    if ([self.delegate respondsToSelector:@selector(pickerArea:selectProvince:selectCity:)]) {
        
        [self.delegate pickerArea:self selectProvince:self.selectProvince selectCity:self.selectCity];
    }
    
    if ([self.delegate respondsToSelector:@selector(pickerArea:selectProvinceId:selectCityId:)]) {
        
        [self.delegate pickerArea:self selectProvinceId:self.selectProvinceId selectCityId:self.selectCityId];
    }
    
    if ([self.delegate respondsToSelector:@selector(pickerArea:selectProvince:selectCity:selectProvinceId:selectCityId:)]) {
        
        [self.delegate pickerArea:self selectProvince:self.selectProvince selectCity:self.selectCity selectProvinceId:self.selectProvinceId selectCityId:self.selectCityId];
    }
    
    [super clickConfirmButton];
}

//获取省市信息
-(void)loadProvinceList{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,previnceApi];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            [self.provinceArray removeAllObjects];
            [self.AllProvinceArray removeAllObjects];
            NSArray *array = response[@"result"];
            for (NSDictionary *dic in array) {
                PrevinceModel *model = [[PrevinceModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.provinceArray addObject:model.provinceName];
                [self.AllProvinceArray addObject:model];
            }
        }
        
        [self.pickerView reloadComponent:0];
        self.selectProvince = self.provinceArray[0];
        PrevinceModel *model = self.AllProvinceArray[0];
        self.selectProvinceId = model.provinceId;
        [self loadCityList:model.provinceId];

    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}
-(void)loadCityList:(NSString *)provinceId{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,cityApi];
    NSDictionary *params = @{@"provinceId":provinceId};
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        
        if ([response[@"code"] isEqual:@1]) {
            [self.cityArray removeAllObjects];
            [self.AllCityArray removeAllObjects];
            NSArray *array = response[@"result"];
            for (NSDictionary *dic in array) {
                CityModel *model = [[CityModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.cityArray addObject:model.cityName];
                [self.AllCityArray addObject:model];
            }
        }
        self.selectCity = self.cityArray[0];
        CityModel *model = self.AllCityArray[0];
        self.selectCityId = model.cityId;
        [self.pickerView reloadComponent:1];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}



- (NSMutableArray *)provinceArray
{
    if (!_provinceArray) {
        _provinceArray = [NSMutableArray array];
    }
    return _provinceArray;
}
-(NSMutableArray *)AllProvinceArray{
    if (!_AllProvinceArray) {
        _AllProvinceArray = [[NSMutableArray alloc] init];
    }
    return _AllProvinceArray;
}

- (NSMutableArray *)cityArray
{
    if (!_cityArray) {
        _cityArray = [NSMutableArray array];
    }
    return _cityArray;
}

-(NSMutableArray *)AllCityArray{
    
    if (!_AllCityArray) {
        _AllCityArray = [[NSMutableArray alloc] init];
    }
    return _AllCityArray;
}


- (NSMutableArray *)selectedArray
{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}



@end
